package com.hsl.study.leetcode;

import java.util.HashMap;

class Solution {
    public static int[] twoSum(int[] nums, int target) {
        HashMap<Integer, Integer> hashMap = new HashMap<>(nums.length);
        for (int i = 0; i < nums.length; i++) {
            //伙伴数
            //{1,2,3,4,5,6,7}
            // 0,1,2,3,4,5,6
            /**
             *  K       V
             *  1       0
             *  2       1
             *  3       2
             *  4       3
             *  5       4
             *  6       5
             *  7       6
             **/

            int number = target - nums[i];
            if (hashMap.containsKey(number)) {
                return new int[]{hashMap.get(number), i};
            }
            hashMap.put(nums[i], i);
        }
        return  null;
    }

    public static void main(String[] args) {
        int[] nums = new int[] {1,2,3,4,5,6,7};
            int target = 10;
        int[] twoSum = twoSum(nums, target);
        for (int i = 0; i < twoSum.length; i++) {
            System.out.println(twoSum[i]);
        }
    }

    public  static  int[] sar(int[] nums, int target){
        for (int i = 0; i <nums.length ; i++) {
            for (int j = i+1; j <nums.length ; j++) {
                if (nums[i] + nums[j] == target){
                    return new int[]{i,j};
                }
            }
        }
        return null;
    }
}